package com.ltb.speedlottery.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class ViewHolder
{
	private final SparseArray<View> mViews;
	private int mPosition;
	private int mGroupPosition = 0;
	private View mConvertView;
    private Context context;
	private ViewHolder(Context context, ViewGroup parent, int layoutId,
                       int position)
	{
		this.mPosition = position;
		this.mViews = new SparseArray<View>();
		mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
				false);
		// setTag
		mConvertView.setTag(this);
		this.context=context;
	}
	
	private ViewHolder(Context context, ViewGroup parent, int layoutId,
                       int groupPosition, int childPosition)
	{
		mGroupPosition = groupPosition;
		this.mPosition = childPosition;
		this.mViews = new SparseArray<View>();
		mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
				false);
		// setTag
		mConvertView.setTag(this);
		this.context=context;
	}

	/**
	 * 拿到一个ViewHolder对象
	 * 
	 * @param context
	 * @param convertView
	 * @param parent
	 * @param layoutId
	 * @param position
	 * @return
	 */
	public static ViewHolder get(Context context, View convertView,
			ViewGroup parent, int layoutId, int position)
	{
		if (convertView == null)
		{
			return new ViewHolder(context, parent, layoutId, position);
		}
		return (ViewHolder) convertView.getTag();
	}
	
	public static ViewHolder get(Context context, View convertView,
			ViewGroup parent, int layoutId, int groupPosition, int childrenPosition)
	{
		if (convertView == null)
		{
			return new ViewHolder(context, parent, layoutId,groupPosition,  childrenPosition);
		}
		return (ViewHolder) convertView.getTag();
	}

	public View getConvertView()
	{
		return mConvertView;
	}

	/**
	 * 通过控件的Id获取对于的控件，如果没有则加入views
	 * 
	 * @param viewId
	 * @return
	 */
	public <T extends View> T getView(int viewId)
	{
		View view = mViews.get(viewId);
		if (view == null)
		{
			view = mConvertView.findViewById(viewId);
			mViews.put(viewId, view);
		}
		return (T) view;
	}

	/**
	 * 为TextView设置字符串
	 * 
	 * @param viewId
	 * @param text
	 * @return
	 */
	public ViewHolder setText(int viewId, String text)
	{
		TextView view = getView(viewId);
		view.setText(text);
		return this;
	}
	
	/**
	 * SpannableStringBuilder 区分不同样式
	 * @param viewId
	 * @param text
	 * @return
	 */
	public ViewHolder setText(int viewId, SpannableString text)
	{
		TextView view = getView(viewId);
		view.setText(text);
		return this;
	}
	public ViewHolder setText(int viewId, SpannableStringBuilder text)
	{
		TextView view = getView(viewId);
		view.setText(text);
		return this;
	}
	
	/**
	 * 为ImageView设置图片
	 * 
	 * @param viewId
	 * @param drawableId
	 * @return
	 */
	public ViewHolder setImageResource(int viewId, int drawableId)
	{
		ImageView view = getView(viewId);
		view.setImageResource(drawableId);

		return this;
	}

	/**
	 * 设置背景图片
	 * 
	 * @param viewId
	 * @param drawableId
	 * @return
	 */
	public ViewHolder setbackground(int viewId, Drawable drawableId)
	{
		TextView view = getView(viewId);
		view.setBackgroundDrawable(drawableId);
		return this;
	}
	/**
	 * 设置背景颜色
	 * 
	 * @param viewId
	 * @param color
	 * @return
	 */
	public ViewHolder setBackgroundColor(int viewId, int color)
	{
		TextView view = getView(viewId);
		view.setBackgroundColor(color);

		return this;
	}
	/**
	 * 设置背景颜色
	 * 
	 * @param viewId
	 * @param color
	 * @return
	 */
	public ViewHolder setTextColor(int viewId, int color)
	{
		TextView view = getView(viewId);
		 view.setTextColor(color);

		return this;
	}
	/**
	 * 为ImageView设置图片
	 * 
	 * @param viewId
	 * @param bm
	 * @return
	 */
	public ViewHolder setImageBitmap(int viewId, Bitmap bm)
	{
		ImageView view = getView(viewId);
		view.setImageBitmap(bm);
		return this;
	}





	
	/**
	 * 为Button设置visibility属性
	 * 
	 * @param viewId
	 * @param visibility
	 * @return
	 */
	public ViewHolder setVisibility(int viewId, int visibility)
	{
		TextView view = getView(viewId);
		view.setVisibility(visibility);
		
		return this;
	}
	/**
	 * 为TextView设置图片属性
	 * 
	 * @param viewId
	 * @param drawableId
	 * @return
	 */
	public ViewHolder setCompoundDrawables(int viewId, Drawable drawableId)
	{
		TextView view = getView(viewId);
		view.setCompoundDrawables(drawableId, null, null, null);
		
		return this;
	}
	
	/**
	 * 为TextView设置是否可点击
	 * 
	 * @param viewId
	 * @param clickable
	 * @return
	 */
	public ViewHolder setClickable(int viewId, Boolean clickable)
	{
		TextView view = getView(viewId);
		view.setClickable(clickable);
		
		return this;
	}

	/**
	 * 设置评分条分数
	 * @param viewId 评分条id
	 * @param rating 分数
	 * @return
	 */
	public ViewHolder setRating(int viewId,float rating){
		RatingBar view = getView(viewId);
		view.setRating(rating);

		return this;
	}
	
	public int getPosition()
	{
		return mPosition;
	}
	
	public int getGroupPosition(){
		return mGroupPosition;
	}
}
